#include "mainmenuwidget.h"
#include <QVBoxLayout>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QFont>
#include <QMessageBox>

MainMenuWidget::MainMenuWidget(QWidget* parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_StyledBackground);
    setWindowTitle("Super Runner");
    setMinimumSize(1200, 900);
    setMaximumSize(1200, 900);
    setupUI();

    // 背景样式设置
    setStyleSheet(
        "MainMenuWidget {"
        "    background-image: url(:/menubg.png);"
        "    background-repeat: no-repeat;"
        "    background-position: center;"
        "    background-size: cover;"
        "}"
        "QLabel, QPushButton {"
        "    background-color: transparent;"
        "}"
        );
}

MainMenuWidget::~MainMenuWidget() {}

void MainMenuWidget::setupUI()
{
    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setAlignment(Qt::AlignCenter);
    mainLayout->setContentsMargins(0, 180, 0, 150);

    // 按钮容器布局
    QVBoxLayout* btnLayout = new QVBoxLayout;
    btnLayout->setSpacing(50);
    btnLayout->setContentsMargins(0, 0, 0, 0);

    // 开始按钮
    startBtn = new QPushButton(this);
    startBtn->setFixedSize(400, 120);
    startBtn->setStyleSheet(
        "QPushButton {"
        "    background-image: url(:/bt1.png);"
        "    background-size: contain;"
        "    border: none;"
        "    background-repeat: no-repeat;"
        "    background-position: center;"
        "}"
        "QPushButton:hover {"
        "    background-image: url(:/bt1.png);"
        "}"
        "QPushButton:pressed {"
        "    background-image: url(:/bt1.png);"
        "}"
        );

    // 规则按钮
    ruleBtn = new QPushButton(this);
    ruleBtn->setFixedSize(400, 120);
    ruleBtn->setStyleSheet(
        "QPushButton {"
        "    background-image: url(:/bt2.png);"
        "    background-size: contain;"
        "    border: none;"
        "    background-repeat: no-repeat;"
        "    background-position: center;"
        "}"
        "QPushButton:hover {"
        "    background-image: url(:/bt2.png);"
        "}"
        "QPushButton:pressed {"
        "    background-image: url(:/bt2.png);"
        "}"
        );

    // 退出按钮
    exitBtn = new QPushButton(this);
    exitBtn->setFixedSize(400, 120);
    exitBtn->setStyleSheet(
        "QPushButton {"
        "    background-image: url(:/bt3.png);"
        "    background-size: contain;"
        "    border: none;"
        "    background-repeat: no-repeat;"
        "    background-position: center;"
        "}"
        "QPushButton:hover {"
        "    background-image: url(:/bt3.png);"
        "}"
        "QPushButton:pressed {"
        "    background-image: url(:/bt3.png);"
        "}"
        );

    // 信号连接
    connect(startBtn, &QPushButton::clicked,
            this, &MainMenuWidget::startGameRequested);
    connect(exitBtn, &QPushButton::clicked,
            qApp, &QApplication::quit);
    connect(ruleBtn, &QPushButton::clicked, [this]() {
        QDialog ruleDialog(this);
        ruleDialog.setWindowTitle(" ");
        ruleDialog.setFixedSize(800, 600);
        ruleDialog.setStyleSheet(
            "QDialog {"
            "    background-image: url(:/rulebg.jpg);"
            "    background-repeat: no-repeat;"
            "    background-position: center;"
            "    background-size: cover;"
            "    border: 2px solid gold;"
            "}"
            );
        ruleDialog.exec();
    });

    // 组合布局
    btnLayout->addWidget(startBtn, 0, Qt::AlignCenter);
    btnLayout->addWidget(ruleBtn, 0, Qt::AlignCenter);
    btnLayout->addWidget(exitBtn, 0, Qt::AlignCenter);

    mainLayout->addSpacing(50);
    mainLayout->addLayout(btnLayout);
}
